package com.fruitshop.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fruitshop.entity.Category;
import com.fruitshop.entity.Fruit;
import com.fruitshop.exception.BusinessException;
import com.fruitshop.mapper.FruitMapper;
import com.fruitshop.service.CategoryService;
import com.fruitshop.service.FruitService;
import com.fruitshop.vo.FruitVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 水果服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FruitServiceImpl extends ServiceImpl<FruitMapper, Fruit> implements FruitService {
    
    private final CategoryService categoryService;
    
    /**
     * 分页查询水果列表
     *
     * @param page       页码
     * @param pageSize   页大小
     * @param categoryId 分类ID
     * @param keyword    关键词
     * @return 水果分页列表
     */
    @Override
    public Page<FruitVO> pageList(Integer page, Integer pageSize, Long categoryId, String keyword) {
        // 创建分页对象
        Page<Fruit> pageParam = new Page<>(page, pageSize);
        
        // 调用Mapper分页查询
        Page<Fruit> fruitPage = baseMapper.selectFruitPage(pageParam, categoryId, keyword);
        
        // 转换为VO对象
        Page<FruitVO> resultPage = new Page<>();
        BeanUtils.copyProperties(fruitPage, resultPage, "records");
        
        // 分类名称映射
        List<FruitVO> fruitVOList = fruitPage.getRecords().stream().map(fruit -> {
            FruitVO fruitVO = new FruitVO();
            BeanUtils.copyProperties(fruit, fruitVO);
            
            // 分类名称
            if (fruit.getCategoryId() != null) {
                try {
                    Category category = categoryService.getCategoryById(fruit.getCategoryId());
                    fruitVO.setCategoryName(category.getTitle());
                } catch (BusinessException e) {
                    fruitVO.setCategoryName("未知分类");
                }
            }
            
            // 处理详情图片
            if (fruit.getDetailImages() != null && !fruit.getDetailImages().isEmpty()) {
                List<String> detailImagesList = Arrays.asList(fruit.getDetailImages().split(","));
                fruitVO.setDetailImages(detailImagesList);
            } else {
                fruitVO.setDetailImages(new ArrayList<>());
            }
            
            return fruitVO;
        }).collect(Collectors.toList());
        
        resultPage.setRecords(fruitVOList);
        return resultPage;
    }
    
    /**
     * 获取水果详情
     *
     * @param id 水果ID
     * @return 水果详情
     */
    @Override
    public FruitVO getDetail(Long id) {
        Fruit fruit = getById(id);
        if (fruit == null) {
            throw new BusinessException("水果不存在");
        }
        
        FruitVO fruitVO = new FruitVO();
        BeanUtils.copyProperties(fruit, fruitVO);
        
        // 分类名称
        if (fruit.getCategoryId() != null) {
            try {
                Category category = categoryService.getCategoryById(fruit.getCategoryId());
                fruitVO.setCategoryName(category.getTitle());
            } catch (BusinessException e) {
                fruitVO.setCategoryName("未知分类");
            }
        }
        
        // 处理详情图片
        if (fruit.getDetailImages() != null && !fruit.getDetailImages().isEmpty()) {
            List<String> detailImagesList = Arrays.asList(fruit.getDetailImages().split(","));
            fruitVO.setDetailImages(detailImagesList);
        } else {
            fruitVO.setDetailImages(new ArrayList<>());
        }
        
        return fruitVO;
    }
    
    /**
     * 获取热门水果列表
     *
     * @param limit 限制数量
     * @return 热门水果列表
     */
    @Override
    public List<FruitVO> listHotFruits(Integer limit) {
        List<Fruit> hotFruits = baseMapper.selectHotFruits(limit);
        
        return hotFruits.stream().map(fruit -> {
            FruitVO fruitVO = new FruitVO();
            BeanUtils.copyProperties(fruit, fruitVO);
            
            // 分类名称
            if (fruit.getCategoryId() != null) {
                try {
                    Category category = categoryService.getCategoryById(fruit.getCategoryId());
                    fruitVO.setCategoryName(category.getTitle());
                } catch (BusinessException e) {
                    fruitVO.setCategoryName("未知分类");
                }
            }
            
            return fruitVO;
        }).collect(Collectors.toList());
    }
    
    /**
     * 获取推荐水果列表
     *
     * @param limit 限制数量
     * @return 推荐水果列表
     */
    @Override
    public List<FruitVO> listRecommendFruits(Integer limit) {
        List<Fruit> recommendFruits = baseMapper.selectRecommendFruits(limit);
        
        return recommendFruits.stream().map(fruit -> {
            FruitVO fruitVO = new FruitVO();
            BeanUtils.copyProperties(fruit, fruitVO);
            
            // 分类名称
            if (fruit.getCategoryId() != null) {
                try {
                    Category category = categoryService.getCategoryById(fruit.getCategoryId());
                    fruitVO.setCategoryName(category.getTitle());
                } catch (BusinessException e) {
                    fruitVO.setCategoryName("未知分类");
                }
            }
            
            return fruitVO;
        }).collect(Collectors.toList());
    }
} 